package com.suyin.ximoon.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtil {
	/**
	 * 将以逗号分隔的字符串转换成字符串数组
	 * @param valStr
	 * @return String[]
	 */
	public static String[] StrList(String valStr){
	    int i = 0;
	    String TempStr = valStr;
	    String[] returnStr = new String[valStr.length() + 1 - TempStr.replace(",", "").length()];
	    valStr = valStr + ",";
	    while (valStr.indexOf(',') > 0)
	    {
	        returnStr[i] = valStr.substring(0, valStr.indexOf(','));
	        valStr = valStr.substring(valStr.indexOf(',')+1 , valStr.length());
	        
	        i++;
	    }
	    return returnStr;
	}
	
	/**
	 * 将list<String> 转为字符串并以，分割
	 * @param stringList
	 * @return
	 */
	public static String listToString(List<String> stringList){
        if (stringList==null) {
            return null;
        }
        StringBuilder result=new StringBuilder();
        boolean flag=false;
        for (String string : stringList) {
            if (flag) {
                result.append(",");
            }else {
                flag=true;
            }
            result.append(string);
        }
        return result.toString();
    }
	
	// 判断是否是卡号
	public static boolean checkSn(String sn) {
		boolean flag = false;
		int result = sn.indexOf("Planc");
		if (result >= 0) {
			flag = true;
		}
		return flag;
	}

	// 判断是否是邮箱
	public static boolean checkEmail(String email) {
		boolean isExist = false;
		Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}");
		Matcher m = p.matcher(email);
		isExist = m.matches();
		return isExist;
	}

	// 判断是否是手机号
	public static boolean checkPhone(String phone) {
		Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[^4,\\D])|(17[0-9])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(phone);
		return m.matches();
	}

	// 非空判断
	public static boolean isNotEmpty(String str) {
		if (str != null && !"".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}
	
	/*判断为空操作*/
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str)) {
			return true;
		} else {
			return false;
		}
	}

	public static String changeNullToEmpty(String str) {
		if (isNotEmpty(str)) {
			return str;
		} else {
			return "";
		}
	}

	// 去除字符串中的空格、回车、换行符、制表符
	public static String replaceBlank(String str)

	{

		Pattern p = Pattern.compile("\\s*|\t|\r|\n");
		Matcher m = p.matcher(str);
		return m.replaceAll("");

	}

	private static String getRandom(int randomNum, int startNum) {
		StringBuffer buffer = new StringBuffer();
		Random random1 = new Random(startNum);
		for (int i = 0; i < randomNum; i++) {
			buffer.append(random1.nextInt());
		}
		return buffer.toString();

	}

	public static Map<String, Object> setMapNullToString(Map<String, Object> map) {
		Map newMap = new HashMap<String, Object>();

		if (map != null) {

			Set keyset = map.keySet();
			Iterator keyiterator = keyset.iterator();
			while (keyiterator.hasNext()) {
				String key = (String) keyiterator.next();
				Object value = map.get(key);
				if (value == null)
					value = " ";
				newMap.put(key, value);
			}

			return newMap;
		} else {
			return newMap;
		}
	}
	
}
